﻿using System;
using System.Collections;
using System.Collections.Generic;
using DimensionsHelper.Common.Contracts.Metadata;

namespace DimensionsHelper.Services.Metadata;

public class MdmUser : IMdmUser
{
    private MdmUser(string name, string fileVersion)
    {
        Name = name;
        FileVersion = fileVersion;
        Comment = string.Empty;
    }

    internal MdmUser(string name, string fileVersion, string comment) :
        this(name, fileVersion)
    {
        Comment = comment;
    }

    public string Name { get; }

    public string FileVersion { get; }

    public string Comment { get; set; }
}


public class MdmUsers : IMdmUsers
{
    private readonly List<MdmUser> _users;

    internal MdmUsers()
    {
        _users = [];
    }

    public IMdmUser this[int index] => _users[index];

    public int Count => _users.Count;

    public IMdmUser AddNew(string name, string fileVersion, string comment)
    {
        var user = new MdmUser(name, fileVersion, comment);
        _users.Add(user);
        return user;
    }

    public void RemoveAt(int index)
    {
        _users.RemoveAt(index);
    }

    #region IEnumerable

    public IEnumerator<IMdmUser> GetEnumerator()
    {
        return _users.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    #endregion
}


public class SaveLog : ISaveLog
{
    internal SaveLog(string fileVersion, int saveCount = 0)
    {
        Date = DateTime.Now;
        MdmFileVersion = fileVersion;
        SaveCount = saveCount;
        VersionSetName = string.Empty;
        UserName = string.Empty;
        Users = new MdmUsers();
    }

    public DateTime Date { get; internal set; }

    public string MdmFileVersion { get; }

    public int SaveCount { get; internal set; }

    public string VersionSetName { get; internal set; }

    public string UserName { get; internal set; }

    public IMdmUsers Users { get; }
}


public class SaveLogs : ISaveLogs
{
    private readonly List<SaveLog> _logs;

    internal SaveLogs()
    {
        _logs = [];
    }

    public ISaveLog this[int index] => _logs[index];

    public int Count => _logs.Count;

    public ISaveLog AddNew(string fileVersion, int saveCount = 0)
    {
        var log = new SaveLog(fileVersion, saveCount);
        _logs.Add(log);
        return log;
    }

    public void Clear()
    {
        _logs.Clear();
    }

    public IEnumerator<ISaveLog> GetEnumerator()
    {
        return _logs.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}